﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using SystemCenterSoa.ApiProject.Controllers;
using SystemCenterSoa.BaseLibrary.Tools.FileTools;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.DtoModel.FileManager;
using SystemCenterSoa.PoModel.Customer;
using SystemCenterSoa.Repository;
using SystemCenterSoa.Repository.CommonLib;

namespace SystemCenterSoa.ApiProject.Area.FileManager
{
    [AllowAnonymous]
    [Route("api/FileManager/[controller]/[action]")]
    public class FileManagerController : MyBaseController
    {


        
        static string configPath = "";
        public FileManagerController(IConfiguration configuration)
        {
            if (string.IsNullOrEmpty(configPath))
            {
                configPath = configuration.GetSection("MyConfig")["FileUploadPath"];
            }
        }
        /// <summary>
        /// 多文件上传
        /// </summary>
        /// <param name="fileList">多个文件</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<List<FileUpLoadResult>>> FilesUpload(List<IFormFile> fileList)
        {
            ResultModel<List<FileUpLoadResult>> result = new ResultModel<List<FileUpLoadResult>>();
            try
            {
                List<FileUpLoadResult> fulList = new List<FileUpLoadResult>();
                if (fileList != null && fileList.Count > 0)
                {


                    foreach (var iFormFile in fileList)
                    {
                        FileUpLoadResult ful = new FileUpLoadResult();
                        //新的文件名
                        string fileName = Guid.NewGuid() + Path.GetExtension(iFormFile.FileName);
                        //新的文件路径
                        string urlPath = configPath + "/" + fileName;
                        //新的文件存储物理路径   FileTools.MapPath类似.NetFramwork 的Server.MapPath();
                        string upLoadPath = FileTools.MapPath("/wwwroot" + configPath);
                        ful.FileOldName = iFormFile.FileName;  //提取上传文件的温家明
                        ful.FileNewName = fileName;
                        ful.FileNewPath = urlPath;
                        ful.FileSize = iFormFile.Length;
                        ful.FileType = iFormFile.ContentType;
                        if (!Directory.Exists(upLoadPath))  //如果目录不存在
                        {
                            Directory.CreateDirectory(upLoadPath); //创建一个目录
                        }
                        try
                        {
                            using (var fs = new FileStream(upLoadPath + "/" + fileName, FileMode.Create))
                            {
                                await iFormFile.CopyToAsync(fs); //把上传文件复制到 fs 文件流中
                                fs.Flush();   //写入到硬盘中
                                ful.IsUpload = true;  //上传成功
                            }

                        }
                        catch (Exception ex)
                        {
                            ful.IsUpload = false;
                        }
                        fulList.Add(ful);


                    }
                    result.code = ResultCode.Ok;
                    result.data = fulList;
                    result.mess = "成功!";

                }
                else
                {
                    result.code = ResultCode.Ok;
                    result.data = null;
                    result.mess = "失败!";
                }




            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;

        }

    }
}
